package com.act;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.common.Constant;
import com.xike.kyt.R;
import com.xike.util.MLog;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class Web_CommonAct extends BaseAct implements OnClickListener {
	WebView webView;
	TextView tv_title;
	TextView tv_back, tv_close;
	ProgressBar pb_loading;

	String strTitle, strUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_webview);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("title")) {
			strTitle = bundle.getString("title");
		}

		if (bundle != null && bundle.containsKey("url")) {
			strUrl = bundle.getString("url");
		}
		initViews();
	}

	@Override
	protected void initViews() {
		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_back.setVisibility(View.VISIBLE);
		tv_back.setOnClickListener(this);
		tv_close = (TextView) findViewById(R.id.layoutTitle_tv_close);
		tv_close.setOnClickListener(this);
		tv_close.setVisibility(View.GONE);

		tv_title = (TextView) findViewById(R.id.headTitle_tvTitle);
		// tv_title.setText(strTitle);

		pb_loading = (ProgressBar) findViewById(R.id.layoutTitle_pb_loading);

		webView = (WebView) findViewById(R.id.webView);
		WebSettings webSettings = webView.getSettings();
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setDefaultTextEncodingName("UTF-8");
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView w, String url) {
				super.onPageFinished(webView, url);
				pb_loading.setVisibility(View.GONE);
				String strTitle = webView.getTitle();
				if (StringUtil.isNotNull(strTitle) && strTitle.length() > 8) {
					strTitle = strTitle.substring(0, 7) + "...";
				}
				tv_title.setText(strTitle);

			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				pb_loading.setVisibility(View.VISIBLE);
				String strTitle = webView.getTitle();
				if (StringUtil.isNotNull(strTitle) && strTitle.length() > 8) {
					strTitle = strTitle.substring(0, 7) + "...";
				}
				tv_title.setText(strTitle);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				if (url.startsWith("mailto:") || url.startsWith("geo:") || url.startsWith("tel:")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					startActivity(intent);
				} else {
					webView.loadUrl(url);
				}
				return true;
			}

		});
		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if (newProgress != 100) {
					pb_loading.setVisibility(View.VISIBLE);
					pb_loading.setProgress(newProgress * 100);
				} else {
					pb_loading.setVisibility(View.GONE);
				}
				super.onProgressChanged(view, newProgress);
			}

		});
		if (StringUtil.isNotNull(strUrl)) {
			MLog.e("url", strUrl);
			webView.loadUrl(strUrl);
		}

	}

	@Override
	protected void updateViews() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.headTitle_tv_back:
			if (webView.canGoBack()) {
				webView.goBack();
				tv_close.setVisibility(View.VISIBLE);
			} else {
				finish();
			}
			break;
		case R.id.layoutTitle_tv_close:
			finish();
			break;
		default:
			break;
		}
	}

}
